package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.common.Result;
import com.game.yxy.entity.CusFile;
import com.game.yxy.entity.CusQ;
import com.game.yxy.entity.GISales;
import com.game.yxy.mapper.GISalesMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/gisales")
@Api(tags = "游戏物品销售管理") // 描述整个控制器的作用
public class GISalesController {
    @Resource
    GISalesMapper gisalesMapper;

    // 添加游戏物品销售记录
    @PostMapping
    @ApiOperation(value = "添加游戏物品销售记录", notes = "创建一个新的游戏物品销售记录")
    public Result<?> save(@RequestBody GISales gisales) {
        gisalesMapper.insert(gisales);
        return Result.success();
    }

    // 更新游戏物品销售记录
    @PutMapping
    @ApiOperation(value = "更新游戏物品销售记录", notes = "根据ID更新游戏物品销售记录")
    public Result<?> update(@RequestBody GISales gisales) {
        gisalesMapper.updateById(gisales);
        return Result.success();
    }

    // 删除游戏物品销售记录
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除游戏物品销售记录", notes = "根据ID删除游戏物品销售记录")
    public Result<?> delete(@ApiParam(value = "游戏物品销售记录ID", required = true) @PathVariable Long id) {
        gisalesMapper.deleteById(id);
        return Result.success();
    }

    // 查询游戏物品销售记录
    @GetMapping
    @ApiOperation(value = "查询游戏物品销售记录", notes = "支持名字、拥有者、游戏名字和关键字查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "名字", defaultValue = "") @RequestParam(defaultValue = "") String itname,
            @ApiParam(value = "拥有者", defaultValue = "") @RequestParam(defaultValue = "") String ownername,
            @ApiParam(value = "游戏名字", defaultValue = "") @RequestParam(defaultValue = "") String Game,
            @ApiParam(value = "搜索关键词", defaultValue = "") @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<GISales> wrapper = Wrappers.<GISales>lambdaQuery();
        if (StrUtil.isNotBlank(itname)) {
            wrapper.like(GISales::getItname, itname);
        }
        if (StrUtil.isNotBlank(ownername)) {
            wrapper.like(GISales::getOwnername, ownername);
        }
        if (StrUtil.isNotBlank(Game)) {
            wrapper.like(GISales::getGame, Game);
        }
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(GISales::getItname, search);
        }
        Page<GISales> gisalesPage = gisalesMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(gisalesPage);
    }

    // 根据ID查询游戏物品销售
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询游戏物品销售", notes = "根据游戏物品销售ID查询游戏物品销售记录")
    public Result<?> findById(@ApiParam(value = "游戏物品销售ID", required = true) @PathVariable Long id){
        GISales giSales = gisalesMapper.selectById(id);
        if (giSales != null) {
            return Result.success(giSales);
        } else {
            return Result.failure("游戏物品销售不存在");
        }
    }
}